package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.banner.model.AddBannerModel;
import com.ls.controller.banner.model.EditBannerModel;
import com.ls.controller.benefit.model.AddBenefitModel;
import com.ls.controller.benefit.model.EditBenefitModel;
import com.ls.controller.benefit.model.SortBenefitModel;
import com.ls.entity.Banner;
import com.ls.entity.Benefit;
import com.ls.entity.SysUser;
import com.ls.enums.RoleEnum;
import com.ls.mapper.BenefitMapper;
import com.ls.service.IBenefitService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 权益 服务实现类
 * </p>
 *
 * @author lanchi
 * @since 2024-10-12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BenefitServiceImpl extends ServiceImpl<BenefitMapper, Benefit> implements IBenefitService {
    /**权益列表*/
    @Override
    public ResultData getPaging() {
        return ResultData.successData(list(new LambdaQueryWrapper<Benefit>().orderByAsc(Benefit::getSort)));
    }
    /** 新增权益 */
    @Override
    public ResultData add(AddBenefitModel model) {
        SysUser sysUser  = SaTokenUtilPy.getSysUser();
        RoleEnum roleEnum = RoleEnum.getRoleEnum(sysUser.getRoleId());
        if (!RoleEnum.ADMIN.equals(roleEnum)) {
            return ResultData.failure("只有管理员才能新增权益");
        }
        //实现新增权益
        Benefit benefit = new Benefit();
        BeanUtils.copyProperties(model, benefit);
        benefit.setCreatorId(sysUser.getUserId());
        benefit.setBenefitId(sysUser.getUserId());
        benefit.setCreateTime(LocalDateTime.now());
        benefit.setUpdateTime(LocalDateTime.now());
        long count = count();
        benefit.setSort((int) (count+1));
        boolean flag = save(benefit);
        if (flag){
            return ResultData.successMsg("新增成功");
        }else {
            return ResultData.failure("新增失败");
        }
    }
    /** 编辑权益 */
    @Override
    public ResultData edit(EditBenefitModel model) {
        SysUser sysUser  = SaTokenUtilPy.getSysUser();
        RoleEnum roleEnum = RoleEnum.getRoleEnum(sysUser.getRoleId());
        if (!RoleEnum.ADMIN.equals(roleEnum)) {
            return ResultData.failure("只有管理员才能修改权益");
        }
        //获取当前权益id判断是否存在
        Benefit benefit = getById(model.getBenefitId());
        if (benefit == null){
            return ResultData.failure("权益不存在");
        }
        //实现修改权益
        BeanUtils.copyProperties(model, benefit);
        benefit.setUpdaterId(sysUser.getUserId());
        benefit.setUpdateTime(LocalDateTime.now());
        boolean flag = updateById(benefit);
        if (flag){
            return ResultData.successMsg("修改成功");
        }else {
            return ResultData.failure("修改失败");
        }
    }
    /** 权益排序 */
    @Override
    public ResultData sort(SortBenefitModel model) {
        SysUser sysUser  = SaTokenUtilPy.getSysUser();
        RoleEnum roleEnum = RoleEnum.getRoleEnum(sysUser.getRoleId());
        if (!RoleEnum.ADMIN.equals(roleEnum)) {
            return ResultData.failure("只有管理员才能修改权益");
        }
        //获取当前权益id判断是否存在
        Benefit benefit = getById(model.getBenefitId());
        if (benefit == null){
            return ResultData.failure("权益不存在");
        }
        //实现修改权益
        BeanUtils.copyProperties(model, benefit);
        benefit.setUpdaterId(sysUser.getUserId());
        benefit.setUpdateTime(LocalDateTime.now());
        boolean flag = updateById(benefit);
        if (flag){
            return ResultData.successMsg("修改成功");
        }else {
            return ResultData.failure("修改失败");
        }
    }
    /** 删除权益 */
    @Override
    public ResultData del(Integer benefitId) {
        Benefit benefit = getById(benefitId);
        if (benefit == null){
            return ResultData.failure("权益不存在");
        }
        return benefit.deleteById()?ResultData.successMsg("删除成功"):ResultData.failure("删除失败");
    }
}
